package com.yqyd.web.controller.nbot.app;


import com.yqyd.common.constant.NbotConstans;
import com.yqyd.common.core.domain.R;
import com.yqyd.common.utils.SecurityUtils;
import com.yqyd.nbot.domain.NbotActType;
import com.yqyd.nbot.domain.NbotActivity;
import com.yqyd.nbot.domain.NbotActivityImages;
import com.yqyd.nbot.domain.NbotActivityRegistrations;
import com.yqyd.nbot.dto.AddActImageto;
import com.yqyd.nbot.dto.JoinActDto;
import com.yqyd.nbot.dto.JoinActSearchDto;
import com.yqyd.nbot.service.INbotActTypeService;
import com.yqyd.nbot.service.INbotActivityImagesService;
import com.yqyd.nbot.service.INbotActivityRegistrationsService;
import com.yqyd.nbot.service.INbotActivityService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/nbot/act")
@CrossOrigin
public class ActApi {

    @Resource
    private INbotActivityService activityService;
    @Resource
    private INbotActTypeService actTypeService;
    @Resource
    private INbotActivityRegistrationsService registrationsService;
    @Resource
    private INbotActivityImagesService imagesService;

    /**
     * 活动类型
     * @return
     */
    @GetMapping("TypeData")
    public R<List<NbotActType>> TypeData() {
        return R.ok(actTypeService.selectNbotActTypeList(new NbotActType()));
    }

    @GetMapping("List")
    public R<List<NbotActivity>> List(NbotActivity activity) {
        activity.setAuditStatus(NbotConstans.ACT_AUDIT_PASS);
        return R.ok(activityService.selectNbotActivityList(activity));
    }

    /**
     * 参与的活动列表
     * @param searchDto
     * @return
     */
    @GetMapping("JoinActList")
    public R<List<NbotActivity>> JoinActList(JoinActSearchDto searchDto) {
        searchDto.setUserId(SecurityUtils.getUserId());
        return R.ok(activityService.selectNbotActivityUserJoinedList(searchDto));
    }

    /**
     * 创建的活动列表
     * @param searchDto
     * @return
     */
    @GetMapping("CreateActList")
    public R<List<NbotActivity>> CreateActList(JoinActSearchDto searchDto) {
        searchDto.setCreatorId(SecurityUtils.getUserId());
        return R.ok(activityService.selectNbotActivityList(new NbotActivity()
               .setCreateId(SecurityUtils.getUserId())
                .setTitle(searchDto.getTitle())
                .setStatus(searchDto.getStatus())));
    }


    /**
     * 活动详情
     * @param id
     * @return
     */
    @GetMapping("Info/{id}")
    public R<Map<String, Object>> Info(@PathVariable("id") Long id) {
        Map<String, Object> map = new HashMap<>();
        // 活动详情
        map.put("Info", activityService.selectNbotActivityById(id));
        // 活动参与者
        map.put("Joiner", registrationsService.selectNbotActivityRegistrationsListByActivityId(id));
        // 活动是否已参加
        map.put("Joined",registrationsService.selectUserJoined(id, SecurityUtils.getUserId()));
        // 参与对象
        map.put("JoinInfo", registrationsService.selectNbotActivityRegistrationsByActivityIdAndUserId(id, SecurityUtils.getUserId()));
        // 活动图片
        map.put("Images", imagesService.selectNbotActivityImagesListByActivityId(id));
        return R.ok(map);
    }

    /**
     * 活动照片上传
     * @param dto
     * @return
     */
    @PostMapping("AddImage")
    public R<String> AddImage(@RequestBody @Valid AddActImageto dto) {
        imagesService.insertNbotActivityImages(new NbotActivityImages()
                .setActivityId(dto.activityId)
                .setImage(dto.getImage())
                .setUserId(SecurityUtils.getUserId())
        );
        return R.ok();
    }

    /**
     * 活动报名
     * @param dto
     * @return
     */
    @PostMapping("Join")
    public R<String> Join(@RequestBody @Valid JoinActDto dto) {
        int rows = registrationsService.insertNbotActivityRegistrations(new NbotActivityRegistrations()
                .setActivityId(dto.getActivityId())
                .setUserId(SecurityUtils.getUserId())
                .setPhone(dto.getPhone())
                .setRealName(dto.getRealName())
                .setPaymentAmount(dto.getAmount()));
        if(rows == 0){
            return R.fail("你已报名");
        }
        return R.ok();
    }


}


